<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.interfaces.xinri.duss.utils.InvokeHelper" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%
    User user = HrmUserVarify.getUser(request, response);
    String userid = user.getUID() + "";
    BaseBean baseBean = new BaseBean();
    JSONObject jsonData = new JSONObject();
    int formid = Math.abs(Util.getIntValue(request.getParameter("formid")));
    String requestid = Util.null2String(request.getParameter("requestid"));
    baseBean.writeLog(">>>>>updateSfoath.jsp>>>>>formid=" + formid+",requestid="+requestid);
    String nodeid = Util.null2String(request.getParameter("nodeid"));
    String remark = Util.null2String(request.getParameter("remark"));
    String workcode = getWorkcodeById(userid);
    baseBean.writeLog(">>>>>updateSfoath.jsp>>>>>nodeid=" + nodeid+",remark="+remark+",workcode="+workcode);
    boolean backFlag = approvalHistoryBack(requestid, formid,workcode, nodeid, remark);
    boolean resultFlag = approvalReject(requestid, formid, "hly001");
    baseBean.writeLog(">>>>>updateSfoath.jsp>>>>>resultFlag=" + resultFlag+",backFlag="+backFlag);
    if (backFlag && resultFlag) {
        jsonData.put("flag", true);
    } else {
        jsonData.put("flag", false);
    }
    out.print(jsonData.toJSONString());
%>
<%!


    private static boolean approvalReject(String requestid, int formid, String gh1) {
        BaseBean baseBean = new BaseBean();
        boolean flag = false;
        RecordSet rs = new RecordSet();
        String tablename = "formtable_main_" + formid;
        String fullDate = DateUtil.getFullDate();
        String src = "reject";
        String clientIp = "";
        baseBean.writeLog(">>>>>updateSfoath.jsp>>>>>requestid=" + requestid);
        try {
            JSONObject paramjson = new JSONObject();
            rs.executeQuery("select dh,entityType from " + tablename + " where requestid=?", requestid);
            rs.next();
            String dh = Util.null2String(rs.getString("dh"));
            String entityType = Util.null2String(rs.getString("entityType"));
            paramjson.put("approvalTxt", "不通过");
            paramjson.put("operator", gh1);
            paramjson.put("approver", gh1);
            paramjson.put("entityType", entityType);
            paramjson.put("businessCode", dh);
            paramjson.put("createdDate", InvokeHelper.dateFormat(fullDate));
            paramjson.put("lastModifiedDate", InvokeHelper.dateFormat(fullDate));
            baseBean.writeLog(">>>>>updateSfoath.jsp>>>>>paramjson=" + paramjson);
            String resultStr = InvokeHelper.reject(paramjson.toJSONString());
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("jkqqcs", paramjson.toJSONString());
            dataMap.put("jkxyfh", resultStr);
            dataMap.put("khdip", clientIp);
            dataMap.put("gllc", requestid);
            dataMap.put("oacz", src);
            dataMap.put("jkdysj", fullDate);
            dataMap.put("djh", dh);
            dataMap.put("rqsj", DateUtil.getDateTime());
            dataMap.put("jkmc", "2");
            dataMap.put("lcid", requestid);
            InvokeHelper.insertApiLog(dataMap, "Reject");
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            baseBean.writeLog(">>>>>WeaApprovalsRejectAction>>>>>resultJson=" + resultJson);
            String message = com.weaver.general.Util.null2String(resultJson.getString("message"));
            if ("operation completed".equals(message)) {
                flag = true;
            } else {
                baseBean.writeLog(">>>>>updateSfoath.jsp>>>>>汇联易接口返回异常：" + message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static String getWorkcodeById(String resourceid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select workcode from hrmresource where id=?", resourceid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    private static boolean approvalHistoryBack(String requestid, int formid,String gh, String nodeid, String remark) {
        BaseBean baseBean = new BaseBean();
        boolean flag = false;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        String tablename = "formtable_main_" + formid;
        String fullDate = DateUtil.getFullDate();
        String src = "reject";
        String clientIp = "";
        try {
            JSONObject paramjson = new JSONObject();
            rs.executeQuery("select dh,entityType from " + tablename + " where requestid=?", requestid);
            rs.next();
            String dh = Util.null2String(rs.getString("dh"));
            String entityType = Util.null2String(rs.getString("entityType"));
            paramjson.put("entityType", entityType);
            paramjson.put("entityOID", "");
            paramjson.put("businessCode", dh);
            paramjson.put("operationType", 1002);
            paramjson.put("operation", 2002);
            paramjson.put("operatorOID", "");
            rs1.executeQuery("select max(concat(operatedate,' ',operatetime)) datetime,operator,remark from workflow_requestlog \n" +
                    "where requestid=? and nodeid=? and logtype=3 group by operator", requestid, nodeid);
            int counts = rs1.getCounts();
            int count = 0;
            while (rs1.next()) {
                String datetime = Util.null2String(rs1.getString("datetime"));
                String operator = Util.null2String(rs1.getString("operator"));
                String remark1 = Util.null2String(rs1.getString("remark"));
                paramjson.put("operator", InvokeHelper.getWorkcodeById(operator));
                paramjson.put("operationDetail", InvokeHelper.removeHTMLTags(remark1));
                paramjson.put("createdDate", InvokeHelper.dateFormat(datetime));
                paramjson.put("lastModifiedDate", InvokeHelper.dateFormat(datetime));
                baseBean.writeLog(">>>>>WeaApprovalHistoryBackAction>>>>>paramjson=" + paramjson);
                String resultStr = InvokeHelper.approvalHistory(paramjson.toJSONString());
                Map<String, Object> dataMap = new HashMap<String, Object>();
                dataMap.put("jkqqcs", paramjson.toJSONString());
                dataMap.put("jkxyfh", resultStr);
                dataMap.put("khdip", clientIp);
                dataMap.put("gllc", requestid);
                dataMap.put("oacz", src);
                dataMap.put("jkdysj", datetime);
                dataMap.put("djh", dh);
                dataMap.put("rqsj", DateUtil.getDateTime());
                dataMap.put("jkmc", "1");
                dataMap.put("lcid", requestid);
                InvokeHelper.insertApiLog(dataMap, "ApprovalHistory");
                JSONObject resultJson = JSONObject.parseObject(resultStr);
                baseBean.writeLog(">>>>>WeaApprovalHistoryBackAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("operation completed".equals(message)) {
                    count++;
                }
            }
            baseBean.writeLog(">>>>>WeaApprovalHistoryAction>>>>>counts=" + counts + ",count=" + count);
            if (count == counts) {
                paramjson.put("operator", gh);
                paramjson.put("operationDetail", InvokeHelper.removeHTMLTags(remark));
                paramjson.put("createdDate", InvokeHelper.dateFormat(fullDate));
                paramjson.put("lastModifiedDate", InvokeHelper.dateFormat(fullDate));
                baseBean.writeLog(">>>>>WeaApprovalHistoryBackAction>>>>>paramjson=" + paramjson);
                String resultStr = InvokeHelper.approvalHistory(paramjson.toJSONString());
                Map<String, Object> dataMap = new HashMap<String, Object>();
                dataMap.put("jkqqcs", paramjson.toJSONString());
                dataMap.put("jkxyfh", resultStr);
                dataMap.put("khdip", clientIp);
                dataMap.put("gllc", requestid);
                dataMap.put("oacz", src);
                dataMap.put("jkdysj", fullDate);
                dataMap.put("djh", dh);
                dataMap.put("rqsj", DateUtil.getDateTime());
                dataMap.put("jkmc", "1");
                dataMap.put("lcid", requestid);
                InvokeHelper.insertApiLog(dataMap, "ApprovalHistory");
                JSONObject resultJson = JSONObject.parseObject(resultStr);
                baseBean.writeLog(">>>>>WeaApprovalHistoryBackAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("operation completed".equals(message)) {
                    flag = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
%>